package com.zrfintech.manage.service;

import com.zrfintech.domain.MgUserRole;
import com.zrfintech.manage.vo.request.UserRoleRequest;
import com.zrfintech.service.MgUserRoleRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class MgUserRoleService {

    @Autowired
    private MgUserRoleRepo mgUserRoleRepo;

    @Transactional(rollbackFor = Exception.class)
    public int batchUpsert(UserRoleRequest userRoleRequest) {
        String userCode = userRoleRequest.getUserCode();
        int deleteFlag = mgUserRoleRepo.deleteByUserCode(userCode);
        if (deleteFlag > 0) {
            for (String roleCode : userRoleRequest.getRoleCodeList()) {
                MgUserRole mgUserRole = new MgUserRole();
                mgUserRole.setUserCode(userCode);
                mgUserRole.setRoleCode(roleCode);
                mgUserRoleRepo.create(mgUserRole);
            }
        }
        return deleteFlag;
    }
}
